package com.itany.corejava.day11;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Random;

public class CodeImage {

    private JFrame frame;

    private JLabel label;

    public CodeImage() {

        frame=new JFrame("验证码");
        label=new JLabel();
    }

    private void init(){
        frame.setLayout(new FlowLayout());

        frame.add(label);
    }


    private void addEventHandler(){
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                generateImage();
            }
        });
    }



    private void show(){
        init();
        addEventHandler();
        generateImage();

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new CodeImage().show();
    }



    private void generateImage(){

        Random random=new Random();


        //在内存中创建一个画图版
        BufferedImage image=new BufferedImage(70,30,BufferedImage.TYPE_INT_RGB);

        //获得画笔
        Graphics graphics=image.getGraphics();

        //设置画笔颜色
        graphics.setColor(new Color(random.nextInt(256),
                random.nextInt(256),
                random.nextInt(256)));


        //画一个带填充色的矩形作为背景
        graphics.fillRect(0,0,70,30);

        //设置画笔颜色
        graphics.setColor(new Color(random.nextInt(256),
                random.nextInt(256),
                random.nextInt(256)));

        //设置画笔字体
        graphics.setFont(new Font("Courier New",Font.BOLD+Font.ITALIC,18));

        //生成一个5位随机字符串，字符串中包括[a-z][A-Z][0-9]
        String s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        StringBuilder code=new StringBuilder();
        for (int i = 0; i < 5; i++) {
            int index=random.nextInt(s.length());
            code.append(s.charAt(index));
        }
        //将文字写到图片上
        graphics.drawString(code.toString(),5,20);

        //画3条干扰线
        for (int i = 0; i < 3; i++) {
            //设置画笔颜色
            graphics.setColor(new Color(random.nextInt(256),
                                        random.nextInt(256),
                                        random.nextInt(256)));

            //画线
            graphics.drawLine(random.nextInt(71),
                              random.nextInt(31),
                              random.nextInt(71),
                              random.nextInt(31));
        }

        //画3个干扰点
        for (int i = 0; i < 3; i++) {
            //设置画笔颜色
            graphics.setColor(new Color(random.nextInt(256),
                                        random.nextInt(256),
                                        random.nextInt(256)));

            //画圆
            graphics.fillOval(random.nextInt(71),
                              random.nextInt(31),
                       3,
                      3);
        }



        label.setIcon(new ImageIcon(image));

    }




}
